import {postAction} from '@/request';
import createAppForModal from '@/utils/createAppForModal';
import {h} from 'vue';
import UserManualSelect from '@comp/UserManualSelect/UserManualSelect.vue';

export default function createFlowService() {
  return {
    preStart(params, callback = (userManualSelect) => ({})) {
      let {formModel, formTypeId, formId} = params;
      postAction('/flow/preStartProcess', params).then(userManualSelect => {
        userManualSelect = userManualSelect.result
        formModel = formModel || {};
        formModel.userManualSelect = userManualSelect;
        formModel.id = formId;

        if (userManualSelect && userManualSelect.select && userManualSelect.select.length) {
          let app = createAppForModal(h(UserManualSelect, {
            userManualSelect,
            onOk: userManualSelect => {
              formModel.userManualSelect = userManualSelect;
              callback && callback(formModel);
              app._instance.ctx.close()
            },
            onCancel: () => {
              app._instance.ctx.close()
            }
          }), {
            style: {
              height: '100vh',
              minHeight: '100vh',
              maxHeight: '100vh',
            },
            props: {
              position: 'right',
            }
          });
          return;
        }
        callback && callback(formModel);
      })
    }
  };
};